package wb.is.besti.edu.cd.emergency_responsesys.pages;

import android.media.Image;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import wb.is.besti.edu.cd.emergency_responsesys.R;
import wb.is.besti.edu.cd.emergency_responsesys.utils.Constant;
import wb.is.besti.edu.cd.emergency_responsesys.utils.Options;
import wb.is.besti.edu.cd.emergency_responsesys.widget.OptionsAdapter;

public class UserFragment extends Fragment implements AdapterView.OnItemClickListener {

    private ImageView mHBack,mHHead;
    private List<Options> optionsList;
    private ListView listView;
    private OptionsAdapter adapter;
    String mainMsg;
    private View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.userpage,container,false);
        this.view = view;
        MyAsyncTask myAsyncTask = new MyAsyncTask();
        myAsyncTask.execute(Constant.URL_PERSONAL);
        return view;
    }
    private void initUI(View view){
        mHBack = (ImageView)view.findViewById(R.id.h_back);//背景UI控件
        mHHead = (ImageView)view.findViewById(R.id.h_head);//头像UI控件
        //设置背景磨砂效果
        Glide.with(this).load(R.drawable.head)
                .bitmapTransform(new BlurTransformation(getContext(), 25), new CenterCrop(getContext()))
                .into(mHBack);
        //设置圆形图像
        Glide.with(this).load(R.drawable.head)
                .bitmapTransform(new CropCircleTransformation(getContext()))
                .into(mHHead);

        //ListView的处理
        listView = (ListView)view.findViewById(R.id.lv_options);
        adapter = new OptionsAdapter(getContext(),R.layout.user_iv_item,optionsList);
        listView.setAdapter(adapter);
        //设定监听器
        listView.setOnItemClickListener(UserFragment.this);
    }
    private void initData(){
        String[] msgs =mainMsg.split("!END!");
        System.out.println("MainActivity_msgs.length="+msgs.length);
        optionsList = new ArrayList<>();
        for (int mIndex=0;mIndex<msgs.length;mIndex++){
            String[] msg = msgs[mIndex].split("_");
            String msg_type = msg[0];
            System.out.println("MainActivity_msg_type="+msg_type);
            String msg_content = "  "+msg[1];
            String msg_time = msg[2];
            Options option = new Options();
            if (msg_type.equals("新闻")){
                option.setOptionsImage(R.drawable.news);
            }else if (msg_type.equals("通知")){
                option.setOptionsImage(R.drawable.notification);
            }
            option.setOptionsText(msg_type);
            option.setContent(msg_content);
            option.setTime(msg_time);
            optionsList.add(option);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

    }
    public  class MyAsyncTask extends AsyncTask<String, Integer, String> {
        String message ="";//反馈的消息

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection = null;
            StringBuilder response = new StringBuilder();
            try {
                URL url = new URL(params[0]); // 声明一个URL,注意如果用百度首页实验，请使用https开头，否则获取不到返回报文
                connection = (HttpURLConnection) url.openConnection(); // 打开该URL连接
                connection.setRequestMethod("GET"); // 设置请求方法，“POST或GET”，我们这里用GET，在说到POST的时候再用POST
                connection.setConnectTimeout(80000); // 设置连接建立的超时时间
                connection.setReadTimeout(80000); // 设置网络报文收发超时时间
                InputStream in = connection.getInputStream();  // 通过连接的输入流获取下发报文，然后就是Java的流处理
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response.toString(); // 这里返回的结果就作为onPostExecute方法的入参

        }

        @Override
        protected void onPostExecute(String s) {
            message = s;
            mainMsg = message;
            System.out.println("MainActivity_message:"+message);
            initData();
            initUI(UserFragment.this.view);
        }
    }
}
